﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using ARCam3D.Transform;
using ARCam3D.Tuple;

namespace ARCam3D.Imaging.Processing
{
    class SampleImageChecker
    {
        private double threshold = 50;
        private Bitmap sample;
        private double markerSideLength;
        private CameraProjectionTransform cameraProjectionTransform;
        

        public SampleImageChecker(Bitmap sample, double markerSideLength, CameraProjectionTransform cameraProjectionTransform)
        {
            this.sample = sample;
            this.markerSideLength = markerSideLength;
            this.cameraProjectionTransform = cameraProjectionTransform;
        }

        public bool CheckSample(RGBImage image, MatrixTransform3D transform, Point2I[] corners)
        {
            double diff = 0;

            for (int x = 0; x < sample.Width; ++x)
            {
                for (int y = 0; y < sample.Height; ++y)
                {
                    Point3D point = new Point3D(markerSideLength * x / sample.Width, markerSideLength * y / sample.Height, 0);
                    transform.Transform(point);
                    Point2I point2I = cameraProjectionTransform.GetProjection(point);
                    RGBColor color = image.GetColor(x, y);
                    Color sampleColor = sample.GetPixel(x, y);

                    diff += Math.Sqrt(
                        (color.red - sampleColor.R) * (color.red - sampleColor.R) +
                        (color.green - sampleColor.G) * (color.green - sampleColor.G) +
                        (color.blue - sampleColor.B) * (color.blue - sampleColor.B)
                    );
                }
            }

            diff /= sample.Width * sample.Height;

            return diff < threshold;
        }
    }
}
